/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.Region;
import com.thinkgem.jeesite.modules.guobo.service.RegionService;

/**
 * 区域Controller
 * @author wudavid
 * @version 2017-03-31
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/region")
public class RegionController extends BaseController {

	@Autowired
	private RegionService regionService;
	
	@ModelAttribute
	public Region get(@RequestParam(required=false) String id) {
		Region entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = regionService.get(id);
		}
		if (entity == null){
			entity = new Region();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:region:view")
	@RequestMapping(value = {"list", ""})
	public String list(Region region, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Region> page = regionService.findPage(new Page<Region>(request, response), region); 
		model.addAttribute("page", page);
		return "modules/guobo/regionList";
	}

	@RequiresPermissions("guobo:region:view")
	@RequestMapping(value = "form")
	public String form(Region region, Model model) {
		model.addAttribute("region", region);
		return "modules/guobo/regionForm";
	}

	@RequiresPermissions("guobo:region:edit")
	@RequestMapping(value = "save")
	public String save(Region region, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, region)){
			return form(region, model);
		}
		regionService.save(region);
		addMessage(redirectAttributes, "保存区域成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/region/?repage";
	}
	
	@RequiresPermissions("guobo:region:edit")
	@RequestMapping(value = "delete")
	public String delete(Region region, RedirectAttributes redirectAttributes) {
		regionService.delete(region);
		addMessage(redirectAttributes, "删除区域成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/region/?repage";
	}

	/**
	 * 获取区域JSON数据
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:region:view")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Region> list = regionService.findList(new Region());
		for (int i=0; i<list.size(); i++){
			Region e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", "0");
			map.put("pIds", "0,");
			map.put("name", e.getName());
			//map.put("isParent", true);
			mapList.add(map);
		}
		return mapList;
	}
}